package com.baselibrary.extension

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.widget.Toast
import com.baselibrary.utils.CommonUtil
import com.baselibrary.utils.HKLogUtils
import com.baselibrary.utils.HKToastUtils

fun String.logD(TAG: String = "HanKol") {
    HKLogUtils.dTag(TAG, this)
}

fun String?.toast(length: Int = Toast.LENGTH_SHORT) {
    this?.let {
        when (length) {
            Toast.LENGTH_LONG -> HKToastUtils.showToast(it)
            else -> HKToastUtils.showToast(it)
        }
    }
}

fun String.decode(): String {
    var decodeString = this
    if (decodeString.contains("&amp;")) {
        decodeString = decodeString.replace("&amp;", "&")
    }
    if (decodeString.contains("&quot;")) {
        decodeString = decodeString.replace("&quot;", "\"")
    }
    if (decodeString.contains("&gt;")) {
        decodeString = decodeString.replace("&gt;", ">")
    }
    if (decodeString.contains("&lt;")) {
        decodeString = decodeString.replace("&lt;", "<")
    }
    return decodeString
}

fun String?.checkValue(): String {
    return CommonUtil.null2Length0(this)
}
/**
 *获取一段文字在文字中的范围
 * @param target
 * @return
 */
fun CharSequence.range(target: CharSequence): IntRange {
    val start = this.indexOf(target.toString())
    return start..(start + target.length)
}
/**
 *设置文字颜色
 * @param range
 * @return
 */
fun CharSequence.foregroundColorSpan(range: IntRange, color: Int = Color.RED): CharSequence {
    return SpannableStringBuilder(this).apply {
        setSpan(ForegroundColorSpan(color), range.first, range.last, Spannable. SPAN_INCLUSIVE_EXCLUSIVE)
    }
}
/**
 *
 */
fun String.subStr(subDigit: Int): String {
    return if (this.length > subDigit) {
        this.substring(0, subDigit) + "......"
    } else {
        this
    }
}